﻿var MsgBox = (function($){
	var title_default = '提示信息！';

	var alert = function(title, msg) {
		if (title === '' || title === null || title === undefined)
			title = title_default;
		generateMsgBoxHTML('alert', title, msg);
		$('body').css('overflow', 'hidden');
		btnCancel();
	};
	var confirm = function(title, msg, callback) {
		if (title === '' || title === null || title === undefined)
			title = title_default;
		generateMsgBoxHTML('confirm', title, msg);
		$('body').css('overflow', 'hidden');
		btnContinue('continue', callback);
		btnCancel();
	};
	var custom = function(title, msg, btnNames, callbacks) {
		if (title === '' || title === null || title === undefined)
			title = title_default;
		generateMsgBoxHTML('custom', title, msg, btnNames, callbacks);
		$('body').css('overflow', 'hidden');
		for (var key in callbacks) {
			btnContinue(key, callbacks[key]);
		}
		btnCancel();
	};
	var btnContinue = function(className, callback) {
		if (className !== 'continue') {
			className = 'call_' + className;
		}
		$('.' + className).click(function() {
			$('#msgbox').remove();
			$('body').css('overflow', 'visible');
			if (typeof (callback) === 'function') {
				callback();
			}
		})
	};
	var btnCancel = function() {
		$('.msgbox-close, .cancel').click(function() {
			$('#msgbox').remove();
			$('body').css('overflow', 'visible');
		})
	};
	var generateMsgBoxHTML = function(type, title, msg, btnNames, callbacks) {
		//var popup_html = "";
		var popup_html = new Array();
		popup_html.push("<div id=\"msgbox\">");
		popup_html.push("<div class=\"mask\"></div>");
		popup_html.push("<div class=\"main-body\">");
		popup_html.push("<div class=\"msgbox-head\"><div class=\"msgbox-title\">");
		popup_html.push(title);
		popup_html.push("</div><div class=\"msgbox-close\"></div></div>");
		popup_html.push("<div class=\"clearfix msgbox-msg\">");
		popup_html.push(msg);
		popup_html.push("</div>");
		popup_html.push("<div class=\"msgbox-bar\">");
		if (type === "alert") {
			popup_html.push("<input class=\"btn cancel\" type=\"button\" value=\"确定\" />");
		} else if (type === "confirm") {
			popup_html.push("<input class=\"btn continue\" type=\"button\" value=\"确定\" />");
			popup_html.push("<input class=\"btn cancel\" type=\"button\" value=\"取消\" />");
		} else if (type === "custom") {
			var i = 0;
			for (var key in callbacks) {
				popup_html.push("<input class=\"btn call_" + key + "\" type=\"button\" value=\"" + btnNames[i] + "\" />");
				i++;
			}
		}
		popup_html.push("</div></div></div>");
		$('body').append(popup_html.join(""));
		generateCss();
	};
	var generateCss = function() {
		$('#msgbox .mask').css({width: '100%',height: '100%',left: '0',top: '0',position: 'fixed',zIndex: '998',background: 'rgba(0, 0, 0, 0.2)'});
		$('#msgbox .main-body').css({width: '300px',height: '170px',marginLeft: '-150px',marginTop: '-85px',left: '50%',top: '50%',position: 'fixed',zIndex: '999999',background: '#fff',fontSize: '12px',color: 'rgb(94,94,94)'});
		$('#msgbox .msgbox-head').css({height: '32px',borderBottom: '1px solid #ccc'});
		$('#msgbox .msgbox-title').css({lineHeight: '32px',marginLeft: '5px',float: 'left'});
		$('#msgbox .msgbox-close').css({cursor: 'pointer',float: 'right',fontSize: '28px',color: 'rgb(172, 172, 172)',fontFamily: 'NSimsun'});
		$('#msgbox .msgbox-close').append("\u00D7");
		$('#msgbox .msgbox-close').hover(
			function () {
				$(this).css({color: '#000'});
			}, function () {
				$(this).css({color: 'rgb(172, 172, 172)'});
			}
		);
		$('#msgbox .msgbox-msg').css({padding: '0 20px 10px 20px',textAlign: 'center',height: '70px',lineHeight: '70px', clear: 'both'});
		$('#msgbox .msgbox-bar').css({textAlign: 'center'});
		$('#msgbox .btn').css({width: '70px',height: '30px',margin: '0 10px'});
	}
	return {
		alert: alert,
		confirm: confirm,
		custom: custom
	}
})(jQuery);
